// SPDX-License-Identifier: GPL-2.0-or-later

#include <linux/kernel.h>
#include <linux/errno.h>
#include "cts_strerror.h"

#define ERROR_TEXT_ENTRY(errno, err_desc_str) \
    [(errno)] = #errno ", " err_desc_str

static char *_error_text[] = {
    [0] = "Success",
    ERROR_TEXT_ENTRY(EPERM, "Operation not permitted"),
    ERROR_TEXT_ENTRY(ENOENT, "No such file or directory"),
    ERROR_TEXT_ENTRY(ESRCH, "No such process"),
    ERROR_TEXT_ENTRY(EINTR, "Interrupted system call"),
    ERROR_TEXT_ENTRY(EIO, "Input/output error"),
    ERROR_TEXT_ENTRY(ENXIO, "No such device or address"),
    ERROR_TEXT_ENTRY(E2BIG, "Argument list too long"),
    ERROR_TEXT_ENTRY(ENOEXEC, "Exec format error"),
    ERROR_TEXT_ENTRY(EBADF, "Bad file descriptor"),
    ERROR_TEXT_ENTRY(ECHILD, "No child processes"),
    ERROR_TEXT_ENTRY(EAGAIN, "Resource temporarily unavailable"),
    ERROR_TEXT_ENTRY(ENOMEM, "Cannot allocate memory"),
    ERROR_TEXT_ENTRY(EACCES, "Permission denied"),
    ERROR_TEXT_ENTRY(EFAULT, "Bad address"),
    ERROR_TEXT_ENTRY(ENOTBLK, "Block device required"),
    ERROR_TEXT_ENTRY(EBUSY, "Device or resource busy"),
    ERROR_TEXT_ENTRY(EEXIST, "File exists"),
    ERROR_TEXT_ENTRY(EXDEV, "Invalid cross-device link"),
    ERROR_TEXT_ENTRY(ENODEV, "No such device"),
    ERROR_TEXT_ENTRY(ENOTDIR, "Not a directory"),
    ERROR_TEXT_ENTRY(EISDIR, "Is a directory"),
    ERROR_TEXT_ENTRY(EINVAL, "Invalid argument"),
    ERROR_TEXT_ENTRY(EMFILE, "Too many open files"),
    ERROR_TEXT_ENTRY(ENFILE, "Too many open files in system"),
    ERROR_TEXT_ENTRY(ENOTTY, "Inappropriate ioctl for device"),
    ERROR_TEXT_ENTRY(ETXTBSY, "Text file busy"),
    ERROR_TEXT_ENTRY(EFBIG, "File too large"),
    ERROR_TEXT_ENTRY(ENOSPC, "No space left on device"),
    ERROR_TEXT_ENTRY(ESPIPE, "Illegal seek"),
    ERROR_TEXT_ENTRY(EROFS, "Read-only file system"),
    ERROR_TEXT_ENTRY(EMLINK, "Too many links"),
    ERROR_TEXT_ENTRY(EPIPE, "Broken pipe"),
    ERROR_TEXT_ENTRY(EDOM, "Numerical argument out of domain"),
    ERROR_TEXT_ENTRY(ERANGE, "Numerical result out of range"),
    ERROR_TEXT_ENTRY(EDEADLK, "Resource deadlock avoided"),
    ERROR_TEXT_ENTRY(ENAMETOOLONG, "File name too long"),
    ERROR_TEXT_ENTRY(ENOLCK, "No locks available"),
    ERROR_TEXT_ENTRY(ENOSYS, "Function not implemented"),
    ERROR_TEXT_ENTRY(ENOTEMPTY, "Directory not empty"),
    ERROR_TEXT_ENTRY(ELOOP, "Too many levels of symbolic links"),
    ERROR_TEXT_ENTRY(EWOULDBLOCK, "Operation would block"),
    ERROR_TEXT_ENTRY(ENOMSG, "No message of desired type"),
    ERROR_TEXT_ENTRY(EIDRM, "Identifier removed"),
    ERROR_TEXT_ENTRY(ECHRNG, "Channel number out of range"),
    ERROR_TEXT_ENTRY(EL2NSYNC, "Level 2 not synchronized"),
    ERROR_TEXT_ENTRY(EL3HLT, "Level 3 halted"),
    ERROR_TEXT_ENTRY(EL3RST, "Level 3 reset"),
    ERROR_TEXT_ENTRY(ELNRNG, "Link number out of range"),
    ERROR_TEXT_ENTRY(EUNATCH, "Protocol driver not attached"),
    ERROR_TEXT_ENTRY(ENOCSI, "No CSI structure available"),
    ERROR_TEXT_ENTRY(EL2HLT, "Level 2 halted"),
    ERROR_TEXT_ENTRY(EBADE, "Invalid exchange"),
    ERROR_TEXT_ENTRY(EBADR, "Invalid request descriptor"),
    ERROR_TEXT_ENTRY(EXFULL, "Exchange full"),
    ERROR_TEXT_ENTRY(ENOANO, "No anode"),
    ERROR_TEXT_ENTRY(EBADRQC, "Invalid request code"),
    ERROR_TEXT_ENTRY(EBADSLT, "Invalid slot"),
    ERROR_TEXT_ENTRY(EDEADLOCK, "File locking deadlock error"),
    ERROR_TEXT_ENTRY(EBFONT, "Bad font file format"),
    ERROR_TEXT_ENTRY(ENOSTR, "Device not a stream"),
    ERROR_TEXT_ENTRY(ENODATA, "No data available"),
    ERROR_TEXT_ENTRY(ETIME, "Timer expired"),
    ERROR_TEXT_ENTRY(ENOSR, "Out of streams resources"),
    ERROR_TEXT_ENTRY(ENONET, "Machine is not on the network"),
    ERROR_TEXT_ENTRY(ENOPKG, "Package not installed"),
    ERROR_TEXT_ENTRY(EREMOTE, "Object is remote"),
    ERROR_TEXT_ENTRY(ENOLINK, "Link has been severed"),
    ERROR_TEXT_ENTRY(EADV, "Advertise error"),
    ERROR_TEXT_ENTRY(ESRMNT, "Srmount error"),
    ERROR_TEXT_ENTRY(ECOMM, "Communication error on send"),
    ERROR_TEXT_ENTRY(EPROTO, "Protocol error"),
    ERROR_TEXT_ENTRY(EMULTIHOP, "Multihop attempted"),
    ERROR_TEXT_ENTRY(EDOTDOT, "RFS specific error"),
    ERROR_TEXT_ENTRY(EBADMSG, "Bad message"),
    ERROR_TEXT_ENTRY(EOVERFLOW, "Value too large for defined data type"),
    ERROR_TEXT_ENTRY(ENOTUNIQ, "Name not unique on network"),
    ERROR_TEXT_ENTRY(EBADFD, "File descriptor in bad state"),
    ERROR_TEXT_ENTRY(EREMCHG, "Remote address changed"),
    ERROR_TEXT_ENTRY(ELIBACC, "Can not access a needed shared library"),
    ERROR_TEXT_ENTRY(ELIBBAD, "Accessing a corrupted shared library"),
    ERROR_TEXT_ENTRY(ELIBSCN, ".lib section in a.out corrupted"),
    ERROR_TEXT_ENTRY(ELIBMAX, "Attempting to link in too many shared libraries"),
    ERROR_TEXT_ENTRY(ELIBEXEC, "Cannot exec a shared library directly"),
    ERROR_TEXT_ENTRY(EILSEQ, "Invalid or incomplete multibyte or wide character"),
    ERROR_TEXT_ENTRY(ERESTART, "Interrupted system call should be restarted"),
    ERROR_TEXT_ENTRY(ESTRPIPE, "Streams pipe error"),
    ERROR_TEXT_ENTRY(EUSERS, "Too many users"),
    ERROR_TEXT_ENTRY(ENOTSOCK, "Socket operation on non-socket"),
    ERROR_TEXT_ENTRY(EDESTADDRREQ, "Destination address required"),
    ERROR_TEXT_ENTRY(EMSGSIZE, "Message too long"),
    ERROR_TEXT_ENTRY(EPROTOTYPE, "Protocol wrong type for socket"),
    ERROR_TEXT_ENTRY(ENOPROTOOPT, "Protocol not available"),
    ERROR_TEXT_ENTRY(EPROTONOSUPPORT, "Protocol not supported"),
    ERROR_TEXT_ENTRY(ESOCKTNOSUPPORT, "Socket type not supported"),
    ERROR_TEXT_ENTRY(EOPNOTSUPP, "Operation not supported"),
    ERROR_TEXT_ENTRY(EPFNOSUPPORT, "Protocol family not supported"),
    ERROR_TEXT_ENTRY(EAFNOSUPPORT, "Address family not supported by protocol"),
    ERROR_TEXT_ENTRY(EADDRINUSE, "Address already in use"),
    ERROR_TEXT_ENTRY(EADDRNOTAVAIL, "Cannot assign requested address"),
    ERROR_TEXT_ENTRY(ENETDOWN, "Network is down"),
    ERROR_TEXT_ENTRY(ENETUNREACH, "Network is unreachable"),
    ERROR_TEXT_ENTRY(ENETRESET, "Network dropped connection on reset"),
    ERROR_TEXT_ENTRY(ECONNABORTED, "Software caused connection abort"),
    ERROR_TEXT_ENTRY(ECONNRESET, "Connection reset by peer"),
    ERROR_TEXT_ENTRY(ENOBUFS, "No buffer space available"),
    ERROR_TEXT_ENTRY(EISCONN, "Transport endpoint is already connected"),
    ERROR_TEXT_ENTRY(ENOTCONN, "Transport endpoint is not connected"),
    ERROR_TEXT_ENTRY(ESHUTDOWN, "Cannot send after transport endpoint shutdown"),
    ERROR_TEXT_ENTRY(ETOOMANYREFS, "Too many references: cannot splice"),
    ERROR_TEXT_ENTRY(ETIMEDOUT, "Connection timed out"),
    ERROR_TEXT_ENTRY(ECONNREFUSED, "Connection refused"),
    ERROR_TEXT_ENTRY(EHOSTDOWN, "Host is down"),
    ERROR_TEXT_ENTRY(EHOSTUNREACH, "No route to host"),
    ERROR_TEXT_ENTRY(EALREADY, "Operation already in progress"),
    ERROR_TEXT_ENTRY(EINPROGRESS, "Operation now in progress"),
    ERROR_TEXT_ENTRY(ESTALE, "Stale file handle"),
    ERROR_TEXT_ENTRY(EUCLEAN, "Structure needs cleaning"),
    ERROR_TEXT_ENTRY(ENOTNAM, "Not a XENIX named type file"),
    ERROR_TEXT_ENTRY(ENAVAIL, "No XENIX semaphores available"),
    ERROR_TEXT_ENTRY(EISNAM, "Is a named type file"),
    ERROR_TEXT_ENTRY(EREMOTEIO, "Remote I/O error"),
    ERROR_TEXT_ENTRY(EDQUOT, "Disk quota exceeded"),
    ERROR_TEXT_ENTRY(ENOMEDIUM, "No medium found"),
    ERROR_TEXT_ENTRY(EMEDIUMTYPE, "Wrong medium type"),
    ERROR_TEXT_ENTRY(ECANCELED, "Operation canceled"),
    ERROR_TEXT_ENTRY(ENOKEY, "Required key not available"),
    ERROR_TEXT_ENTRY(EKEYEXPIRED, "Key has expired"),
    ERROR_TEXT_ENTRY(EKEYREVOKED, "Key has been revoked"),
    ERROR_TEXT_ENTRY(EKEYREJECTED, "Key was rejected by service"),
    ERROR_TEXT_ENTRY(EOWNERDEAD, "Owner died"),
    ERROR_TEXT_ENTRY(ENOTRECOVERABLE, "State not recoverable"),
    ERROR_TEXT_ENTRY(ERFKILL, "Operation not possible due to RF-kill"),
    ERROR_TEXT_ENTRY(EHWPOISON, "Memory page has hardware error"),
    ERROR_TEXT_ENTRY(ERESTARTSYS, "Restart system"),
    ERROR_TEXT_ENTRY(ERESTARTNOINTR, "Restart monitor"),
    ERROR_TEXT_ENTRY(ERESTARTNOHAND, "Restart if no handler"),
    ERROR_TEXT_ENTRY(ENOIOCTLCMD, "No ioctl command"),
    ERROR_TEXT_ENTRY(ERESTART_RESTARTBLOCK, "Restart by calling sys_restart_syscall"),
    ERROR_TEXT_ENTRY(EPROBE_DEFER, "Driver requests probe retry"),
    ERROR_TEXT_ENTRY(EOPENSTALE, "Open found a stale dentry"),
    ERROR_TEXT_ENTRY(EBADHANDLE, "Illegal NFS file handle"),
    ERROR_TEXT_ENTRY(ENOTSYNC, "Update synchronization mismatch"),
    ERROR_TEXT_ENTRY(EBADCOOKIE, "Cookie is stale"),
    ERROR_TEXT_ENTRY(ENOTSUPP, "Operation is not supported"),
    ERROR_TEXT_ENTRY(ETOOSMALL, "Buffer or request is too small"),
    ERROR_TEXT_ENTRY(ESERVERFAULT, "An untranslatable error occurred"),
    ERROR_TEXT_ENTRY(EBADTYPE, "Type not supported by server"),
    ERROR_TEXT_ENTRY(EJUKEBOX, "Request initiated, but will not complete before timeout"),
    ERROR_TEXT_ENTRY(EIOCBQUEUED, "IOCB queued, will get completion event"),
};

#define ERROR_TEXT_ARRAY_SIZE (sizeof(_error_text) / sizeof(_error_text[0]))
char *cts_strerror(int errno)
{
    if (errno > 0) {
        return "<valid offset/length>";
    } else if (-errno < (int) ERROR_TEXT_ARRAY_SIZE) {
        char *s = _error_text[-errno];
        if (s) {
            return s;
        }
    }
    return "<Unknown error>";
}
